<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>宿舍批量更新</title>
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/apartmentViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dormitoryAllocateAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/buildingsMgrAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/page.js'></script>
		<script type="text/javascript">
		    var sql;
	        var totalcount = -1; //查询到的总行数
	        var page;   //dwr分页的page对象，注意在页面中是一个Json对象
	        var dormCache ={};
	        var apartmentid;
	        var dormallocationid; // 宿舍批量更新的预分配id
	        var dormitoryIdList = new   Array();
		//页面初始化
		$j(function(){
			setApartList();
			setAllocationInfo();
		});
		 //载入系统公寓信息
		function setApartList() {
			apartmentViewAction.getAllApartInfo(mycallback);
		}
		var mycallback = function setList(aptValue)//回调函数
		{
			//alert(aptValue[0].apartmentfloornum);
			var s = $("aptName");
			 	DWRUtil.removeAllOptions(s);  
				s.options[0] = new Option("请选择公寓",'0');
			for ( var i = 1; i < aptValue.length+1; i++) {
				var opt = new Option(aptValue[i-1].apartmentname,
						aptValue[i-1].apartmentid);
				s.options[i] = opt;
			}
		}

		//载入预分配allcationinfo的内容
		function setAllocationInfo(){
			return dormitoryAllocateAction.getAllAllocationInfo(aicallback);
		}
		var aicallback = function aicall_back(aiList){
			var sp = $("updateassign");
			DWRUtil.removeAllOptions(sp);  
			sp.options[0] = new Option("预分配值",'0');
			for ( var i = 1; i < aiList.length+1; i++) {
				var opt = new Option(aiList[i-1].allocationname,
						aiList[i-1].allocationid);
				sp.options[i] = opt;
			}
		}
	
		function updateassignSelectChange(){
				apartmentid = $("aptName").options[$("aptName").selectedIndex].value; //给apartmentid赋值
				dormallocationid = $("updateassign").options[$("updateassign").selectedIndex].value; //给dormallocationid赋值
		}
				
				
	//查询总行数
	function searchTotalCount(){
		if(apartmentid==undefined||apartmentid==0){
			alert("请选择公寓！");
			return;
		}
		if($('MyTable').style.visibility == 'visible'){
			$('MyTable').style.visibility = 'hidden';
		}
		if($('updatediv').style.visibility == 'visible'){
			$('updatediv').style.visibility = 'hidden';
		}
		buildingsMgrAction.getApartmentDormTotalRowsByID(apartmentid,tcbck); //查询宿舍
	}
	
	var tcbck = function (bckcount){
		totalcount = bckcount; // 给总行数赋值
		sql = "select d.dormitoryid,d.dormitoryname,d.dormitorydisplayname,d.dormitorysex,count(sbl.dormitorybedid) as inamout,d.dormitorylodgetype,ai.allocationname from dormitory d left join studentbedlink sbl on d.dormitoryid=sbl.dormitoryid left join allocationinfo ai on ai.allocationid = d.allocationid"
			+" where d.apartmentid = " + apartmentid +" group by d.dormitoryid order by d.dormitorysort";
		page("pageShow",sql,0,30,totalcount,function(newsql){
			buildingsMgrAction.ListAllDormListPerPageByApartmentID(newsql,renderdormcallback);
		});
		
	}
	
	var renderdormcallback  = function (dormlst){
		if(dormlst==null){
			alert("查询到的宿舍列表为空！");
		return;
		}
		if($('MyTable').style.visibility == 'hidden'){
			$('MyTable').style.visibility = 'visible';
		}
		if($('updatediv').style.visibility == 'hidden'){
			$('updatediv').style.visibility = 'visible';
		}
		
	DWRUtil.removeAllRows('ttb', { filter:function(tr) {
		return (tr.id != "pattern");
	}});
	var dom,id;
	for (var i =0; i < dormlst.length ; i++) {
		dom = dormlst[i];
		id = dom[0];
		//alert(id);
		dwr.util.cloneNode("pattern", { idSuffix:id });
		dwr.util.setValue("tableid"+id, dom[0]);//隐藏项
		dwr.util.setValue("tablename"+id, dom[1]);
		dwr.util.setValue("tabledisplayname"+id, dom[2]);
		if(dom[3]==1){
			dwr.util.setValue("tablesex"+id,'男');
		}else{
			dwr.util.setValue("tablesex"+id,'女');
		}
		dwr.util.setValue("tableinamount"+id,dom[4]+'/'+dom[5]);
		dwr.util.setValue("tableroomtype"+id,dom[5]);
		dwr.util.setValue("tableprevalue"+id,dom[6]);
		dwr.util.setValue("cbx" + id, dom[0]);
		$("pattern" + id).style.display= "";
		dormCache[id] = dom;
	}
	var Ptr= document.getElementById('MyTable').getElementsByTagName('tr'); 
	for (i=1;i<Ptr.length+1;i++) {
	    Ptr[i-1].className = (i%2>0)?"row_odd":"row_even";
	} 
	judgeCheckboxInNewPage();
	tbCSS();
	}
	
//批量更新宿舍
function updatebatchdorm(){
	if(dormallocationid==""){
		alert("请选择标记值!");
		return;
	}
	if(dormitoryIdList==""){
		alert("请选择宿舍!");
		return;
	}else{
		//alert(dormitoryIdList);
		buildingsMgrAction.updateBatchDormitoryPreassignInfo(dormitoryIdList+"",dormallocationid,updatecbck);
		//使所有已勾选的checkbox不勾选上
		var  a=document.getElementsByTagName("input");  
		for(var i=0;i<a.length;i++){  
		    if(a[i].type=='checkbox' && a[i].id != "checkbox_62") { 
		    	if(a[i].checked == true){
		       		a[i].checked = false;
		       		checkCheckboxInDormitoryIdList(a[i]);
		       	}
		    }
		}
	}
}
var updatecbck = function update_cbck(status){
	alert(status);
	dormitoryIdList.length = 0;
	searchTotalCount();
	
}

//当页全选
function selectCurrentPage(){
	var   a=document.getElementsByTagName("input");  
	if($('checkbox_62').checked == true){
	 	for(var i=0;i<a.length;i++){  
	       if(a[i].type=='checkbox' && a[i].id != "checkbox_62") { 
	       		a[i].checked = true ;
	       		checkCheckboxInDormitoryIdList(a[i]);
	       	}
	    }
    }
    else if($('checkbox_62').checked == false){
	    for(var i=0;i<a.length;i++){  
		    if(a[i].type=='checkbox' && a[i].id != "checkbox_62") { 
		       	a[i].checked = false;
		       	checkCheckboxInDormitoryIdList(a[i]);
		    }
		}
    }
}


function checkDormitoryIdList(str){
	for(var a=0 ;a<dormitoryIdList.length;a++){
		if(str == dormitoryIdList[a]){
			return a;
		}else continue;
	}
	return -1;
}
function checkCheckboxInDormitoryIdList(obj){
	var p = checkDormitoryIdList(obj.id.substr(3));
	if(p == -1 && obj.checked == true){
		dormitoryIdList[dormitoryIdList.length] = obj.id.substr(3);
	}
	if(p != -1 && obj.checked == false){
		dormitoryIdList.splice(p,1);
	}
}
//dwr分页中，当进入到新的页面时判断当前页的checkbox是否曾经被选中，并存入了dormitoryIdList中
function judgeCheckboxInNewPage(){
	var a=document.getElementsByTagName("input");  
   	for(var i=0;i<a.length;i++){  
         if(a[i].type=='checkbox'){  
             if(checkDormitoryIdList(a[i].id.substr(3)) != -1){
             	 a[i].checked = true;
             }else a[i].checked = false;
         }
    }
}


</script>
	</head>
	<body >
		<div id="page_container">
			<%@include file="mgrbanner.jsp"%>
			<h2>
				标记房间
			</h2>
			<div class="divground">
				<div id="search_title">
					<b class="rtop"><b class="r1"></b><b class="r2"></b><b
						class="r3"></b><b class="r4"></b> </b>
					<span><select class="common_select" name="aptName"
							id="aptName" style="width: 120"
							onchange="javascript:updateassignSelectChange();"></select> </span>
					<span><input class="common_button" type="button" value="提交"
							onclick="javascript:searchTotalCount();" /> </span>
					<span><input type="checkbox" name="checkbox_62"
							id="checkbox_62" onclick="selectCurrentPage()" />当页全选</span>
					<b class="rbottom"><b class="r4"></b><b class="r3"></b><b
						class="r2"></b><b class="r1"></b> </b>
				</div>
				<!-- 隐藏的项目开始 -->
				<div id="tableid" style="display: none"></div>
				<!-- 隐藏的项目结束-->
				<table id="MyTable" class="table_common table_list_style"
					style="visibility: hidden;">
					<thead>
						<tr>
							<th width="6%">
								<div>
									选择
								</div>
							</th>
							<th width="24%">
								<div>
									公寓名称
								</div>
							</th>
							<th width="24%">
								<div>
									宿舍显示名
								</div>
							</th>
							<th width="9%">
								<div>
									性别
								</div>
							</th>
							<th width="10%">
								<div>
									房型
								</div>
							</th>
							<th width="14%">
								<div>
									已住/总数
								</div>
							</th>
							<th width="14%">
								<div>
									预分配值
								</div>
							</th>
						</tr>
					</thead>
					<tbody id="ttb">
						<tr id="pattern" style="display: none;">
							<td>
								<div>
									<input type="checkbox" id="cbx" name="cbx" value="checkbox"
										onclick="checkCheckboxInDormitoryIdList(this)" />
								</div>
							</td>
							<td>
								<div id="tablename">
								</div>
							</td>
							<td>
								<div id="tabledisplayname">
								</div>
							</td>
							<td>
								<div id="tablesex">
								</div>
							</td>
							<td>
								<div id="tableroomtype">
								</div>
							</td>
							<td>
								<div id="tableinamount">
								</div>
							</td>
							<td>
								<div id="tableprevalue">
								</div>
							</td>
						</tr>
					</tbody>
				</table>
				<div id="pageShow">
				</div>
				<div id="updatediv" style="visibility: hidden;">
					<label>
						标记为:
					</label>
					<select class="common_select" name="updateassign" id="updateassign"
						style="width: 120" onchange="updateassignSelectChange()">
					</select>
					<input class="common_button" type="submit" value="确定"
						onclick="javascript:updatebatchdorm();" />
				</div>
			</div>
		</div>
	</body>
</html>
